layui.use(['form', 'table','layuimini','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoMusic = {
        id: 'BoMusicTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'title', title: '歌名', align: 'center'},
            {field: 'artist', title: '歌手名', align: 'center'},
            {field: 'poster', title: '封面图', align: 'center',
                templet: function (object) {
                    if(object.poster == '' || object.poster == null){
                        return '-';
                    }else{
                        return '<img src="/layuimini/images/' + object.poster +'"';
                    }
                }
            },
            {field: 'isShow', title: '是否显示', align: 'center', width: 100,
                templet: function (object) {
                    if(object.isShow == 0){
                        return '<span class="layui-badge layui-bg-green">是</span>';
                    }else{
                        return '<span class="layui-badge">否</span>';
                    }
                }
            },
            {field: 'sort', title: '排序', align: 'center'},
            {field: 'size', title: '音乐大小', align: 'center',
                templet: function (object) {
                    var size = object.size / 1024 / 1024;
                    return size.toFixed(3) + 'M';
                }
            },
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            {field: 'createDate', title: '上传时间', minWidth: 120, align: 'center'},
            {templet: '#stateBtn', width: 205, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoMusic.id,
        url: '/web/boMusic/recycleList',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    /**
     * 点击彻底删除按钮
     * @param id
     */
    BoMusic.thoroughlyDelete = function(id){
        layer.open({
            title: '彻底删除音乐'
            ,content: '是否确定彻底删除该音乐？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boMusic/thoroughlyDelete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoMusic.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };

    /**
     * 点击恢复按钮
     * @param id
     */
    BoMusic.restore = function(id){
        layer.open({
            title: '恢复音乐'
            ,content: '是否确定恢复该音乐？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boMusic/restore';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoMusic.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };

    //音乐预览
    BoMusic.playMusic = function(data) {

        var imgHtml = '<div style="margin: 30px 0 0 50px;">' +
            '<audio controls>' +
            '<source src="/music/' + data.mp3 + '" type="audio/mpeg">' +
            '<embed src="/music/' + data.mp3 + '">你的浏览器不支持该音频</audio>' +
            '<p>歌名：' + data.title + '<p>' +
            '<p>歌手：' + data.artist + '<p>' +
            '</div>';

        //捕获页
        var index = layer.open({
            title: '音乐播放',
            type: 1,
            shade: 0.2,
            maxmin:false,
            shadeClose: false,
            area: ['400px', '300px'],
            content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            btn: ['关闭'],
            yes: function(index, layero){
                layer.close(index);
            }
        });
    };

    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoMusic.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'thoroughlyDelete') {
            BoMusic.thoroughlyDelete(data.id);
        }else if(layEvent === 'restore'){
            BoMusic.restore(data.id);
        } else if(layEvent === 'playMusic'){
            BoMusic.playMusic(data);
        }
    });

});